/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BappsLibrary.m
//  bapps
//
//  Created by Nikolaj Tørring on 8/1/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BALibrary.h"
#import "JSONEncodingHelperMethods.h"

@interface BALibrary ()

//providing private access to readonly variables
@property (nonatomic) BOOL isLibraryModeAllowed;
@property (nonatomic) BOOL isLibrarianModeAllowed;
@property (nonatomic) BOOL isIMFModeEnabled;
@property (nonatomic) BOOL isScannerEnabled;

@end

@implementation BALibrary

+ (id)objectWithJSONObject:(NSDictionary *)jsonObj
{    
    BALibrary *obj = [BALibrary new];
    
    obj.materialLocations = [JSONEncodingHelperMethods parseObjectOfClass:[BAMaterialLocations class] withKey:@"locations" fromJsonObject:jsonObj];
    if (obj.materialLocations == nil) {
        //The BALibrary object is cashed as JSON data. When upgrading from a previous version of the app "materialLocations" will not exist initially, this would cause a crash when trying to convert to JSON as the object will me nil. This line will prevent crashes when upgrading from older versions of the app.
        obj.materialLocations = [BAMaterialLocations new];
    }
    
    obj.name = [JSONEncodingHelperMethods parseStringWithKey:@"name" fromJsonObject:jsonObj];
    EXPECT_OBJECT(NSString, obj.name);
    
    obj.customerId = [JSONEncodingHelperMethods parseStringWithKey:@"customerId" fromJsonObject:jsonObj];
    EXPECT_OBJECT(NSString, obj.customerId);
    obj.libraryWebsiteURL = [JSONEncodingHelperMethods parseStringWithKey:@"librarywebsiteurl" fromJsonObject:jsonObj];
    EXPECT_OBJECT(NSString, obj.libraryWebsiteURL);
    
    NSDictionary* logoUrls = [jsonObj objectForKey:@"logoUrls"];
    EXPECT_OBJECT(NSDictionary, logoUrls);
    obj.logoUrls = [JSONEncodingHelperMethods parseDictionaryWithKey:@"iphone" fromJsonObject:logoUrls];
    EXPECT_OBJECT(NSDictionary, obj.logoUrls);
    
    NSDictionary* appURLs = [jsonObj objectForKey:@"appurls"];
    EXPECT_OBJECT(NSDictionary, appURLs);
    obj.appURL = [JSONEncodingHelperMethods parseStringWithKey:@"ios" fromJsonObject:appURLs];
    EXPECT_OBJECT(NSString, obj.appURL);
    
    obj.modules = [JSONEncodingHelperMethods parseArrayOfStringsWithKey:@"modules" fromJsonObject:jsonObj];
    //FIXME: EXPECT_OBJECT(NSArray, obj.modules);
    if (obj.modules==nil) {
        obj.modules = [NSArray new];
    }
    EXPECT_OBJECT(NSArray, obj.modules);
    
    obj.style = [JSONEncodingHelperMethods parseDictionaryWithKey:@"style" fromJsonObject:jsonObj];
    EXPECT_OBJECT(NSDictionary, obj.style);
    
    obj.filters = [jsonObj objectForKey:@"filters"];
    EXPECT_OBJECT(NSDictionary, obj.filters);
    
    //UTILS
    //parse the modules array to allow for quick lookup properties
    obj.isLibraryModeAllowed = [obj.modules containsObject:@"library"];
    obj.isLibrarianModeAllowed = [obj.modules containsObject:@"librarian"];
    obj.isIMFModeEnabled = [obj.modules containsObject:@"imf"];
    obj.isScannerEnabled = [obj.modules containsObject:@"scanner"];
    
    return obj;
}

-(NSDictionary *)toJSONObject{
    //FIXME: temp hack for compatibility with build version 0.1
    if (self.style==nil) {
        self.style = [NSDictionary new];
    }

    //We need to check every object, to ensure the in-line Dictionary below will not fail.
    EXPECT_OBJECT(NSString, self.name);
    EXPECT_OBJECT(NSString, self.customerId);
    EXPECT_OBJECT(NSString, self.libraryWebsiteURL);
    EXPECT_OBJECT(NSDictionary, self.logoUrls);
    EXPECT_OBJECT(NSString, self.appURL);
    EXPECT_OBJECT(NSArray, self.modules);
    EXPECT_OBJECT(NSDictionary, self.style);
    EXPECT_OBJECT(NSDictionary, self.filters);
    EXPECT_OBJECT(BAMaterialLocations, self.materialLocations);
    
    NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:@{
      @"name" : self.name,
      @"customerId" : self.customerId,
      @"librarywebsiteurl" : self.libraryWebsiteURL,
      @"logoUrls" : @{ @"iphone" : self.logoUrls },
      @"appurls" : @{ @"ios" : self.appURL },
      @"filters" : self.filters,
      @"style" : self.style,
      @"modules" : self.modules,
      @"locations" : [self.materialLocations toJSONObject]
    }];
    
    return result;
}

@end
